package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.Bundle;

import com.example.myapplication.R;

import android.os.CountDownTimer;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.utils.Locus;
import com.example.myapplication.utils.Point;

import java.util.ArrayList;
import java.util.List;


public class MapActivity1 extends AppCompatActivity {

    private ImageButton pos;
    private ImageButton search;
    private Button invisible;
    private EditText e_shopname;
    private String shopname;
    private MyView local;
    //private MyView local2;
    private Point point;
    private ImageView imageView;
    private Bitmap bitmap;
    private Button yf401;
    private Button yf402;
    private Button yf403;
    private Button yf404;
    private Button yf405;
    private Button yf406;
    private Button yf408;
    private Button yf409;
    private Button yf410;
    private Button yf411;
    private Button yf413;
    private Button yf414;
    private Button yf415;
    private Button cancel;

    private Locus track;
    private int floor=4;
    private int start;
    private int end;

    float x;
    float y;

    private static final int UPDATE_P = 1;
    private Handler handler;
    private Runnable runnable;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map1);
        local = findViewById(R.id.myView);
        //local2=findViewById(R.id.myView2);
        invisible = (Button) findViewById(R.id.invisibleMap1);
        init();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

   public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
       if(itemId==R.id.f3_item){
           Intent intent1=new Intent(MapActivity1.this,MapActivity2.class);
           startActivity(intent1);
       } else if (itemId==R.id.f5_item) {
           Intent intent2=new Intent(MapActivity1.this,MapActivity3.class);
           startActivity(intent2);
       }
       return true;
    }

    private void init(){

        point = new Point(this,this);
        pos=(ImageButton)findViewById(R.id.select_p01);
        search = (ImageButton)findViewById(R.id.select_p02);
        cancel=(Button)findViewById(R.id.cancel1);
        yf401 = (Button) findViewById(R.id.YF401);
        yf402 = (Button) findViewById(R.id.YF402);
        yf403 = (Button) findViewById(R.id.YF403);
        yf404 = (Button) findViewById(R.id.YF404);
        yf405 = (Button) findViewById(R.id.YF405);
        yf406 = (Button) findViewById(R.id.YF406);
        yf408 = (Button) findViewById(R.id.YF408);
        yf409 = (Button) findViewById(R.id.YF409);
        yf410 = (Button) findViewById(R.id.YF410);
        yf411 = (Button) findViewById(R.id.YF411);
        yf413 = (Button) findViewById(R.id.YF413);
        yf414 = (Button) findViewById(R.id.YF414);
        yf415 = (Button) findViewById(R.id.YF415);

        //按钮与搜索框相连
        e_shopname=(EditText)findViewById(R.id.select01);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<PointF> mylocus=new ArrayList<>();
                local.setNavigationPath(mylocus);
            }
        });
        yf401.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("海底捞");
                end=0;
               // e_shopname.invalidate();
            }
        });

        yf402.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("糯米Tai");
                end=2;
            }
        });

        yf403.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("井格重庆火锅");
                end=3;
            }
        });

        yf404.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("云海肴");
                end=8;
            }
        });

        yf405.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("布歌东京");
                end=11;
            }
        });

        yf406.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("满记甜品");
                end=12;
            }
        });

        yf408.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("汉堡王");
                end=12;
            }
        });

        yf409.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("南京大牌档");
                end=11;
            }
        });

        yf410.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("TASTY西堤");
                end=9;
            }
        });

        yf411.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("俄士厨房");
                end=7;
            }
        });

        yf413.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("汤臣小厨");
                end=3;
            }
        });

        yf414.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("长安记");
                end=2;
            }
        });

        yf415.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                e_shopname.setText("蔡澜港式点心");
                end=1;
            }
        });

        invisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CountDownTimer timer = new CountDownTimer(1000000000, 3000) {
                    @Override
                    public void onTick(long l) {
                        point.getLocation(new Point.LocationCallback() {
                            @Override
                            public void onLocationResult(float[] location) {
                                if (location != null && location.length != 0) {
                                    System.out.println("X:"+x+" Y:"+y);
                                    float new_x = (location[0]/230)*x;
                                    float new_y = (location[1]/680)*y;
                                    System.out.println("local[0]:"+location[0]+"local[1]: "+location[1]);
                                    start = (int)location[2];
                                    local.setUserLocation(new PointF(new_x,new_y));
                                }
                            }
                            @Override
                            public void onLocationError(Exception e) {
                                System.out.println(e);
                            }
                        });
                    }

                    @Override
                    public void onFinish() {
                    }
                }.start();
            }
        });

        /*List<PointF> s=new ArrayList<>();
        PointF a=new PointF(20,30);
        PointF b=new PointF(30,40);
        PointF c=new PointF(40,50);
        s.add(a);
        s.add(b);
        s.add(c);*/
        pos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                track=new Locus(floor,start,end);
                //local.setNavigationPath(s);
              //  CountDownTimer timer = new CountDownTimer(1000000000, 2000) {
                    float x = local.getWidth();
                    float y = local.getHeight();
                    //@Override
               //     public void onTick(long l) {
                        track.getLocation(new Locus.LocationCallback() {
                            @Override
                            public void onLocationResult(List<PointF> locus) {
                                List<PointF> mylocus=new ArrayList<>();
                                int size = locus.size();
                                for (int i = 0; i < size; i++) {
                                    PointF p = locus.get(i);
                                    PointF myp=new PointF((p.x / 230) * x,(p.y / 680) * y);
                                    mylocus.add(myp);
                                }
                                local.setNavigationPath(mylocus);
                            }

                            @Override
                            public void onLocationError(Exception e) {
                                System.out.println(e);
                            }
                        });
                  /*  }

                    @Override
                    public void onFinish() {
                    }
                }.start();*/
            }

        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //搜索商家信息
                shopname=e_shopname.getText().toString().trim();
                Intent intent = new Intent(MapActivity1.this,activity_info.class);
                intent.putExtra("storename",shopname);
                startActivity(intent);

            }
        });


        ViewTreeObserver vto = local.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 在视图树完成测量时获取宽度和高度
                x = local.getWidth();
                y = local.getHeight();
                // 确保只会调用一次
                local.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                // 在需要触发按钮事件的地方调用 performClick()
                invisible.performClick();
            }
        });
    }


}